/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: SequenceMatchingAnalyzer.java 484 2007-02-17 22:13:17Z jacek.kolezynski $
 */
package logmatcher.analyzers;

import logmatcher.Configurable;
import logmatcher.InitializationException;
import logmatcher.LogEventProducer;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;


/**
 *  Class performs pairing of messages. Each pair is identified by some qualifier. Each type in the pair is
 *  distinguished by message content.
 *
 *  @version $Revision: 484 $
 */
public class SequenceMatchingAnalyzer extends MatchingAnalyzer implements Configurable {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    /** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(SequenceMatchingAnalyzer.class);

    //~ Instance fields ------------------------------------------------------------------------------------------------

    /** Contains only unmatched pairs. */
    private Map<String, MatchingElement> unmatchedSequences = new HashMap<String, MatchingElement>();

    /** Contains only unmatched pairs. */
    private Map<String, MatchingElement> matchedSequences = new HashMap<String, MatchingElement>();

    /** Sequence to be analyzed. */
    private Sequence sequence;

    //~ Constructors ---------------------------------------------------------------------------------------------------

/**
     * Creates a new PairMatchingAnalyzer object.
     */
    public SequenceMatchingAnalyzer() {
        super();
        LOGGER.debug("constructor");
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     *  Read the object properties from configuration source.
     *
     *  @see logmatcher.Configurable#configure(org.apache.commons.configuration.Configuration, java.lang.String)
     */
    public void configure(Configuration config, String id)
        throws InitializationException {
        LOGGER.debug("configuring...");

        Object ob = config.getProperty("logmatcher.logbuffer");

        if (ob instanceof LogEventProducer) {
            LogEventProducer logEventProducer = (LogEventProducer)ob;
            logEventProducer.addLogEventListener(this);
        } else {
            throw new InitializationException("Not initialized log buffer at " + "logmatcher.logbuffer");
        }

        String s = config.getString(id + ".id");

        if (s == null) {
            throw new InitializationException("Missing configuration value: " + id + ".id");
        }

        idPattern = Pattern.compile(s);

        String[] sequenceElements = config.getStringArray(id + ".sequence");

        if (sequenceElements == null) {
            throw new InitializationException("Missing configuration value: " + id + ".sequence");
        }

        sequence = new Sequence();

        for (int i = 0; i < sequenceElements.length; i++) {
            sequence.addMessage(sequenceElements[i]);
        }

        showInvalidMessages = config.getBoolean(id + ".showinvalidmessages");
    }


    /**
     *  Returns curretn sequence.
     *
     *  @return sequence used for analysis.
     */
    public Sequence getSequence() {
        return sequence;
    }


    /**
     *  Sets the sequence to be analyzed.
     *
     *  @param sequence sequence to be analyzed
     */
    public void setSequence(Sequence sequence) {
        this.sequence = sequence;
    }


    /**
     *  {@inheritDoc}
     */
    @Override
    public MatchingElement getSequenceCopy(String id) {
        MatchingElement el = (MatchingElement)sequence.cloneWithId(id);
        el.start();
        return el;
    }


    /**
     *  {@inheritDoc}
     */
    @Override
    public Map<String, MatchingElement> getMatchedElements() {
        return matchedSequences;
    }


    /**
     *  {@inheritDoc}
     */
    @Override
    public Map<String, MatchingElement> getUnmatchedElements() {
        return unmatchedSequences;
    }
}
